package com.imessage.action.no.usephone;

import com.bean.BusinessGroup;
import com.bean.BusinessUsePhone;
import com.imessage.action.BaseAction;
import com.imessage.type.BaseState;
import com.imessage.vo.UsePhoneVo;
import io.ebean.Expr;
import io.ebean.Expression;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.servlet.ModelAndView;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

@Slf4j
public class UsephoneBaseAction extends BaseAction {

    protected final String datagrid = "datagrid-businessUsePhone-filter";


    protected void setExpressionList(List<Expression> expressionList, UsePhoneVo usePhoneVo) {
        if (StringUtils.isNotBlank(usePhoneVo.getPhone())) {
            expressionList.add(Expr.contains("phone", usePhoneVo.getPhone()));
        }

        if (StringUtils.isNotBlank(usePhoneVo.getState())) {
            expressionList.add(Expr.eq("state", usePhoneVo.getState()));
        }

        if (StringUtils.isNotBlank(usePhoneVo.getGroupName())) {
            expressionList.add(Expr.contains("BusinessGroup.name", usePhoneVo.getGroupName()));
        }
    }


    protected void setModelAndView(ModelAndView modelAndView, BusinessUsePhone usePhone) {
        if (usePhone.getBusinessGroup() == null) {
            usePhone.setBusinessGroup(new BusinessGroup());
        }
        modelAndView.addObject("businessUsePhoneForm", usePhone);
        List<Expression> expressionList = new ArrayList<>();
        expressionList.add(Expr.eq("state", BaseState.normal.getCode()));
        List<BusinessGroup> businessGroupList = baseService.findList(
                BusinessGroup.class, expressionList);
        modelAndView.addObject("groupList", businessGroupList);
        modelAndView.addObject("statusList", BaseState.values());
    }

    protected void setUsePhone(UsePhoneVo usephoneVo, BusinessUsePhone usePhone) {
        copyProperties(usephoneVo, usePhone);
        if (StringUtils.isNotBlank(usephoneVo.getState())) {
            usePhone.setState(BaseState.fromValue(usephoneVo.getState()));
        }
        if (StringUtils.isBlank(usephoneVo.getBusinessGroupId())) {
            List<Expression> expressionList = new ArrayList<Expression>();
            expressionList.add(Expr.eq("name", "系统默认"));
            expressionList.add(Expr.eq("state", BaseState.normal.getCode()));
            BusinessGroup businessGroup = baseService.findObject(BusinessGroup.class, expressionList);
            if (businessGroup != null) {
                usePhone.setBusinessGroup(businessGroup);
            }
        } else {
            BusinessGroup businessGroup = baseService.findById(
                    BusinessGroup.class, usephoneVo.getBusinessGroupId());
            usePhone.setBusinessGroup(businessGroup);
        }
    }

    protected boolean saveExcel(String filePath) {
        try {
            File file = new File(filePath);
            InputStream is = new FileInputStream(file);
            String fileName = file.getName();
            Workbook hssfWorkbook = null;
            if (fileName.endsWith("xlsx")) {
                hssfWorkbook = new XSSFWorkbook(is);    //Excel 2007
            } else if (fileName.endsWith("xls")) {
                hssfWorkbook = new HSSFWorkbook(is);    //Excel 2003
            }
            // 循环工作表Sheet
            for (int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) {
                Sheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);
                if (hssfSheet == null) {
                    continue;
                }
                List<String> headlist = new ArrayList<String>();
                Row row0 = hssfSheet.getRow(0);// 第一张sheet的第一行
                if (row0 == null) {
                    continue;
                }
                for (int cellNum = 0; cellNum < row0.getLastCellNum(); cellNum++) {
                    Cell cell = row0.getCell(cellNum);
                    headlist.add(cell.getStringCellValue().trim());// 读取
                }
                // 循环行Row
                for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {
                    Row row = hssfSheet.getRow(rowNum);
                    if (row == null) {
                        continue;
                    }
                    BusinessUsePhone businessUsePhone = new BusinessUsePhone();
                    for (int j = 0; j < headlist.size(); j++) { // 对每个单元格进行循环
                        String value = headlist.get(j);// 读取当前单元格的值
                        Cell cell = row.getCell(j);
                        if (cell == null || StringUtils.isBlank(cell.getStringCellValue())) {
                            continue;
                        }
                        String content = cell.getStringCellValue().trim();
                        if (value.toLowerCase().indexOf("分组") != -1) {
                            BusinessGroup businessGroup=baseService.findObject(
                                    BusinessGroup.class, Expr.eq("name", content));
                            if(businessGroup!=null){
                                businessUsePhone.setBusinessGroup(businessGroup);
                            }
                        }
                        if (value.toLowerCase().indexOf("号码") != -1) {
                            businessUsePhone.setPhone(content);
                        }
                        if (value.toLowerCase().indexOf("备注") != -1) {
                            businessUsePhone.setRemark(content);
                        }
                    }
                    BusinessUsePhone product = baseService.findObject(
                            BusinessUsePhone.class, Expr.eq("phone", businessUsePhone.getPhone()));
                    if (product == null) {
                        baseService.saveObject(businessUsePhone);
                    }
                }
            }
        } catch (Exception e) {
            log.error("", e);
            return false;
        }
        return true;
    }


}
